<?php
include '../../../wp-blog-header.php';
$brand_id = '1035';
//$count = 0;

$url = 'http://kuponanindia.ebaysocialapps.com/deals/deals.php?ptr=buzzr';

function download_page($path){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$path);
        curl_setopt($ch, CURLOPT_FAILONERROR,1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        $retValue = curl_exec($ch);
        curl_close($ch);
        return $retValue;
}

$curl_json_output = download_page($url);
$ebay_deals = xml2array($curl_json_output);
//shuffle($ebay_deals['deals']['deal']);
//$ebay_deals = array_slice($ebay_deals['deals']['deal'], 0, 4);

//echo '<pre>';
//print_r($ebay_deals);
//echo '</pre>';
//exit();

foreach($ebay_deals['deals']['deal'] as $key => $value){
		
    $columns_deal['name'] = str_replace("'"," ",pods_sanitize($value['title']));
    $columns_deal['camp_link'] = substr(sanitize_title($columns_deal['name']),0,128);
    $ext_img_url = $value['image']['large'];
	 
    $deal = new Pod('deal');
    $where = 't.camp_link = "'.$columns_deal['camp_link'].'" AND t.daily = "1"';
    $deal->findRecords('id DESC', 1,$where);
    $dealsFound = $deal->getTotalRows();
	$columns_deal['camp_end'] = date('Y-m-d H:i:s',strtotime($value['start_date']));
	
    if(($dealsFound == 0) && (date('Y-m-d H:i:s') < $columns_deal['camp_end']))
    {
	
	  $city_name = $value['city'];
	  unset($city_id);
      $City = new Pod('city');
	  if($city_name == "Delhi/NCR"){
      $City->findRecords('name ASC', -1,"t.name='Delhi'");
    	while($City->fetchRecord())
    	{
      	$city_id  = $City->get_field('id');
    	}
	  }else {
	   $City->findRecords('name ASC', -1,"t.name='Mumbai'");
	   while($City->fetchRecord())
    	{
      	$city_id  = $City->get_field('id');
    	}
	  }
      $columns_deal['city'] = $city_id;
   
	      
      $extlink = $value['product_url'];
      $shortened_url = make_bitly_url($extlink);
      if(empty($shortened_url))
      {
        $shortened_url = make_goog_url($extlink);
      }
      if(empty($shortened_url))
      {
        $columns_deal['extlink'] = $value['product_url']; 
      }
      else{
        $columns_deal['extlink'] = $shortened_url;
      }
	  
      $columns_deal['camp_start'] = date('Y-m-d H:i:s',strtotime($value['end_date']));
      
      $columns_deal['brand'] = $brand_id;
      $columns_deal['daily'] = 1;
	  if(!empty($value['mrp'])){
		$columns_deal['oriprice'] = str_replace('INR ','',$value['mrp']);
	  }
	  if(!empty($value['discount'])){
		$columns_deal['disper'] = $value['discount'];
	  }
	  if(!empty($value['offer_price'])){
		$columns_deal['disprice'] = (floor(str_replace('INR ','',$value['offer_price'])));
	  }
      $columns_deal['ext_image_url'] = $ext_img_url;
	  $cat_id = check_daily_category($columns_deal['name']);
	  if(!empty($cat_id))
	  {
		$columns_deal['camp_cat'] = $cat_id;
	  }
          
      $api = new PodAPI();
      $params = array('datatype' => 'deal', 'columns' => $columns_deal);
      
       
       echo '<pre>';
       print_r($params);
       echo '</pre>';
       //exit();
       
       $api->save_pod_item($params);  
       
    }

}

?>

